package cc.wfu.infrastructure.persistent.repository;

import cc.wfu.domain.user.event.UserLastLoginMessageEvent;
import cc.wfu.domain.user.model.entity.UserEntity;
import cc.wfu.domain.user.repository.IUserRepository;
import cc.wfu.infrastructure.event.EventPublisher;
import cc.wfu.infrastructure.persistent.dao.IUserDao;
import cc.wfu.infrastructure.persistent.po.User;
import cc.wfu.types.common.RoleConstants;
import cc.wfu.types.snow.SnowflakeIdGenerator;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 用户仓储实现
 * @Date: 2024-05-16-15:10
 */
@Repository
public class UserRepository implements IUserRepository {

    @Resource
    private EventPublisher eventPublisher;
    @Resource
    private UserLastLoginMessageEvent lastLoginMessageEvent;

    @Resource
    private SnowflakeIdGenerator snowflakeIdGenerator;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private IUserDao userDao;

    @Override
    public int save(UserEntity userEntity) {

        // 1. 准备参数
        User userReq = User.builder()
                .userId(String.valueOf(snowflakeIdGenerator.nextId()))
                .email(userEntity.getUsername())
                .password(passwordEncoder.encode(userEntity.getPassword()))
                .roleType(RoleConstants.ROLE_USER)
                .state(1)
                .lastLoginTime(null)
                .lastPasswordReset(null)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();

        // 2. 插入数据库
        return userDao.save(userReq);
    }

    /**
     * 通过邮箱查询用户
     * @param email 用户邮箱
     * @return 用户信息
     */
    @Override
    public UserEntity queryUser(String email) {
        // 1. 准备参数
        User userReq = User.builder()
                .email(email)
                .state(1)
                .build();
        // 2. 查询
        User user = userDao.queryUser(userReq);
        // 3. 类型转换
        if (user == null) {
            return null;
        }
        return UserEntity.builder()
                .password(user.getPassword())
                .roleType(user.getRoleType())
                .username(user.getEmail())
                .userId(user.getUserId())
                .build();
    }

    /**
     * 发送 MQ 消息，记录登录时间
     * @param email 用户邮箱
     */
    @Override
    public void sendLoginMqMessage(String email) {
        eventPublisher.publish(lastLoginMessageEvent.topic(), lastLoginMessageEvent.buildEventMessage(email));
    }

    /**
     * 修改上次登录时间
     * @param email 用户邮箱
     */
    @Override
    public void updateLastLoginTime(String email) {
        // 1. 准备参数
        User userReq = User.builder()
                .email(email)
                .state(1)
                .lastLoginTime(LocalDateTime.now())
                .build();
        // 2. 修改
        if (userDao.updateLastLoginTime(userReq) != 1) {
            throw new RuntimeException();
        }
    }

    /**
     * 修改用户密码
     */
    @Override
    public int updatePassword(UserEntity userEntity) {
        // 1. 准备参数
        User userReq = User.builder()
                .email(userEntity.getUsername())
                .state(1)
                .password(userEntity.getPassword())
                .lastPasswordReset(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        // 2. 修改
        return userDao.updatePassword(userReq);
    }
}
